package com.bright.controller.index;


import com.alibaba.fastjson.JSONObject;
import com.bright.dao.BlogDao;
import com.bright.pojo.Comment;
import com.bright.service.BlogService;
import com.bright.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RequestMapping("/index")
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/comments")
    private List<Comment> getComments(Long blogid){
        return commentService.listComment(blogid);
    }

    @PostMapping("/setcomment")
    private String setComment(@RequestBody  JSONObject comment){
        Comment comment1 = JSONObject.toJavaObject(comment,Comment.class);
        Long blogid = comment1.getBlogid();
        comment1.setBlog(blogService.getBlog(blogid));
        System.out.println(commentService.saveCommnet(comment1));
        return "success";
    }

    @GetMapping("/getreplay")
    private List<Comment> getreplays(Long blogid){
        return commentService.getreplayComments(blogid);
    }
}
